package com.mini.rpc.consumer.util;

import com.mini.rpc.consumer.RpcConsumer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author: hliushi
 * @date: 2022/4/24 15:06
 **/
public class PropertiesUtil {

    private static final String path = "application.properties";

    /**
     * 根据key获取value
     *
     * @param key
     * @return value
     */
    public static String getValue(String key) {
        InputStream is = RpcConsumer.class.getClassLoader().getResourceAsStream(path);
        Properties properties = new Properties();
        // todo 优化, 使用 jdk7.0 特性的 try() {} 语法
        try {
            properties.load(is);
            return properties.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                assert is != null;
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
